namespace RuoVea.CodeGen.Server.Service;

using Microsoft.AspNetCore.Mvc;
using RuoVea.CodeGen.Server.Models;

/// <summary>
/// Sys_TableInfo 服务
/// </summary>
[ApiExplorerSettings(GroupName = "V1")]
public partial class SysTableInfoService : IApplicationService
{
    /// <summary>
    /// 数据访问仓储
    /// </summary>
    private readonly SugarRepository<Sys_TableInfo> _thisRepository;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="thisRepository">数据访问仓储</param>
    public SysTableInfoService(SugarRepository<Sys_TableInfo> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data">分页参数</param>
    /// <returns>分页结果</returns>
    public async Task<RestfulResult<PageResult<Sys_TableInfo>>> GetPagesAsync(PageParam? data)
    {
        var restful = new RestfulResult<PageResult<Sys_TableInfo>>() { Code = CodeStatus.OK };
        data ??= new PageParam { PageNo = 1, PageSize = 10 };
        var query = await _thisRepository.AsQueryable()
            .Includes(t => t.TableColumns) // 加载表列信息
            .ToPageAsync(data.PageNo, data.PageSize);

        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 获取所有表信息
    /// </summary>
    /// <returns>表信息列表</returns>
    public async Task<RestfulResult<List<Sys_TableInfo>>> GetlistAsync()
    {
        var restful = new RestfulResult<List<Sys_TableInfo>>() { Code = CodeStatus.OK };
        var query = await _thisRepository.AsQueryable()
             .Includes(t => t.TableColumns) // 加载表列信息
             .Select(u => u)
            .ToListAsync();
        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 增加表信息
    /// </summary>
    /// <param name="data">表信息</param>
    /// <returns>操作结果</returns>
    [UnitOfWork]
    public async Task<RestfulResult> AddAsync(Sys_TableInfo data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新表信息
    /// </summary>
    /// <param name="data">表信息</param>
    /// <returns>操作结果</returns>
    public async Task<RestfulResult> UpdateAsync(Sys_TableInfo data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Table_Id == data.Table_Id);
        if (!isExist)
        {
            restful.Message = "记录不存在";
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data, true, true, "表信息修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除表信息
    /// </summary>
    /// <param name="data">删除参数</param>
    /// <returns>操作结果</returns>
    public async Task<RestfulResult> DeleteAsync([FromBody] dynamic data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        int id = data.Id;
        if (id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
}